<?php
require_once '../sub/init.php';
define('ZEAI2',dirname(__FILE__).DIRECTORY_SEPARATOR);
header("Cache-control: private");
require_once 'chkUadm.php';
if($session_kind == 'crm'){
	if(!in_array('crm_user_grade',$QXARR))exit(noauth());
}
require_once ZEAI.'cache/config_vip.php';
require_once ZEAI.'cache/udata.php';
if(!ifint($uid))callmsg("forbidden","-1");
$urole  = json_decode($_ZEAI['urole'],true);
$row = $db->NUM($uid,'uname,nickname,grade,sjtime,sex,openid,subscribe,sjtime2,crm_qxnum,hh_num,rz_num',"id=$uid");
if(!$row){
	textmsg("此会员不存在或已经锁定！");
}else{
	$uname = dataIO($row[0],'out');
	$nickname = trimhtml(dataIO($row[1],'out'));
	$grade    = $row[2];
	$sjtime   = $row[3];
	$sex      = $row[4];
	$data_openid    = $row[5];
	$data_subscribe = $row[6];
	$sjtime2   = $row[7];
	$crm_qxnum = $row[8];
	$hh_num    = $row[9];
	$rz_num    = $row[10];
	$uname = (empty($uname))?$nickname:$uname;
	$log_str1 = utitle($grade).'：'.YmdHis($sjtime).'－'.YmdHis($sjtime2);
}?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<?php
function is_time($time){
	$pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';
	return preg_match($pattern,$time);
}
function isDateTime($dateTime){
	$ret = strtotime($dateTime);
	return $ret !== FALSE && $ret != -1;
}
if($submitok == "mod"){
	$fgrade = intval($fgrade);
	if ( !ifint($fgrade,'0-9','1,2') )textmsg("等级太大，不要超过20");
	/***crm_qxnum,hh_num***/
	$crm_qxnumARR = json_decode($_VIP['qianxian_num'],true);
	$hh_numARR    = json_decode($_VIP['hh_num'],true);
	$rz_numARR    = json_decode($_VIP['rz_num'],true);
	//if($fgrade>$grade){//升级
		$n2=abs(intval($crm_qxnumARR[$fgrade]));
		//if($n2>0)$SQLNUM1 = ",crm_qxnum=crm_qxnum+$n2";
		$SQLNUM1 = ",crm_qxnum=$n2";
		//
		$n2=abs(intval($hh_numARR[$fgrade]));
		//if($n2>0)$SQLNUM2 = ",hh_num=hh_num+$n2";
		$SQLNUM2 = ",hh_num=$n2";
		//
		$n2=abs(intval($rz_numARR[$fgrade]));
		//if($n2>0)$SQLNUM3 = ",rz_num=rz_num+$n2";
		$SQLNUM3 = ",rz_num=$n2";
	//}
	/*
	elseif($fgrade<$grade){//降级
		$n1=abs(intval($crm_qxnumARR[$grade]));
		$n2=abs(intval($crm_qxnumARR[$fgrade]));
		$endnum1=$crm_qxnum-$n1;
		$SQLNUM1 = ($endnum1>0)?",crm_qxnum=$endnum1":",crm_qxnum=$n2";

		$n1=abs(intval($hh_numARR[$grade]));
		$n2=abs(intval($hh_numARR[$fgrade]));
		$endnum2=$hh_num-$n1;
		$SQLNUM2 = ($endnum2>0)?",hh_num=$endnum2":",hh_num=$n2";

		$n1=abs(intval($rz_numARR[$grade]));
		$n2=abs(intval($rz_numARR[$fgrade]));
		$endnum3=$rz_num-$n1;
		$SQLNUM3 = ($endnum3>0)?",rz_num=$endnum3":",rz_num=$n2";
	}
	*/
	if($fgrade==1){
		$db->query("UPDATE ".__TBL_USER__." SET grade=1,sjtime=0,sjtime2=0".$SQLNUM1.$SQLNUM2.$SQLNUM3." WHERE id=".$uid);
		//日志
		AddLog('修改用户等级【'.$nickname.'（uid:'.$uid.'）】->'.utitle($fgrade));
	}else{
		$fsjtime=trim($fsjtime);
		$fsjtime2=trim($fsjtime2);
		if (!is_time($fsjtime) || !isDateTime($fsjtime))textmsg("【起始时间】格式不对，请检查<br><br>例：".(date("Y")+1)."-01-15 06:48:29<br><br>",'back','返回重写');
		if (!is_time($fsjtime2) || !isDateTime($fsjtime2))textmsg("【结束时间】格式不对，请检查<br><br>例：".(date("Y")+1)."-01-15 06:48:29<br><br>",'back','返回重写');
		$fsjtime  = strtotime($fsjtime);
		$fsjtime2 = strtotime($fsjtime2);
		
		$maxY=ADDTIME+31536000*50;
		if($fsjtime2>$maxY)textmsg("亲，【结束时间】太遥远了吧<br><br><br><br>",'back','返回重写');
		if($fsjtime2<$fsjtime)textmsg("亲，【结束时间】必须大于【起始时间】<br><br><br><br>",'back','返回重写');
		
		$SQL  = " ,sjtime=".$fsjtime.",sjtime2=".$fsjtime2;
		$SQL .= $SQLNUM1.$SQLNUM2.$SQLNUM3;
		
		$db->query("UPDATE ".__TBL_USER__." SET grade=".$fgrade.$SQL." WHERE id=".$uid);
		//日志
		$log_str2 = utitle($fgrade).'：'.YmdHis($fsjtime).'－'.YmdHis($fsjtime2);
		AddLog('修改用户等级【'.$nickname.'（uid:'.$uid.'）】'.$log_str1.'->'.$log_str2);
		
		$sjtips=($fgrade>1)?'，服务期至：'.YmdHis($fsjtime2):'';
		//站内通知
		$C = $nickname.'您好，用户等级变更成功【原：'.utitle($grade).'】->【新：'.utitle($fgrade).$sjtips.'】　　<a href='.a('{"kind":"my","mp":"pc"}').' class=aQING>查看详情</a>';
		$db->SendTip($uid,'用户等级变更成功',dataIO($C,'in'),'sys');
		//微信模版通知
		if (!empty($data_openid) && $data_subscribe==1){
			$keyword1 = '您好，用户等级变更成功【原：'.utitle($grade).'】->【新：'.utitle($fgrade).$sjtips.'】';
			$keyword3 = urlencode($_ZEAI['siteName']);
			$url      = urlencode(a('{"kind":"my","mp":"mob"}'));
			@wx_mb_sent('mbbh=ZEAI_ADMIN_INFO&openid='.$data_openid.'&keyword1='.$keyword1.'&keyword3='.$keyword3.'&time='.ADDTIME.'&first='.$first.'&remark='.$remark.'&url='.$url);
		}
	}
	$endbz ='<a href="javascript:;" class="aHONGed" onClick=zeai.iframe("更改【'.$uname.'】等级","u_grade.php?uid='.$uid.'",400,440)>'.utitle($fgrade).'</a>';
	$returnid = 'grade'.$uid;
?>
<script>
window.parent.document.getElementById('<?php echo $returnid; ?>').innerHTML = '<?php echo $endbz; ?>';
window.parent.zeai.iframe(0);
</script>
<?php exit;}?>

<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
</head>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style type="text/css">
.gradelist{padding:5px 0 10px 15px;border-left:#eee 4px solid;color:#999}
.radioskin:checked + label.radioskin-label b{color:#000}
.www_zeai_cn_form{margin-bottom:40px;margin-top:10px}
.autodate{background-color:#f8f8f8;padding:10px 0;border-bottom:#EEEEEE 1px solid}
.autodate a{display:inline-block;color:#999;line-height:30px;border:#dadada 1px solid;margin:0 6px;padding:0 10px;background-color:#f8f8f8;border-radius:3px;font-size:14px}
.autodate a:hover{background-color:#eee}
</style>
<body class="body">
<div class="autodate" title="点击快速输入下方 ↓ 起始和结束时间 ↓" id="autodate">快速输入：<a d1="<?php echo YmdHis(ADDTIME);?>" d2="<?php echo YmdHis(ADDTIME+86400*30);?>">1个月</a><a d1="<?php echo YmdHis(ADDTIME);?>" d2="<?php echo YmdHis(ADDTIME+86400*90);?>">3个月</a><a d1="<?php echo YmdHis(ADDTIME);?>" d2="<?php echo YmdHis(ADDTIME+86400*180);?>">半年</a><a d1="<?php echo YmdHis(ADDTIME);?>" d2="<?php echo YmdHis(ADDTIME+86400*365);?>">1年</a><a d1="<?php echo YmdHis(ADDTIME);?>" d2="<?php echo YmdHis(ADDTIME+86400*730);?>">2年</a></div>
<form name="ZEAI_FORM" method="POST" action="<?php echo SELF;?>" class="www_zeai_cn_form">
<table align="center" cellpadding="5" cellspacing="5" class=" W90_">
    <tr>
        <td width="90" align="right" class="S14">VIP起始时间 </td>
      <td align="left" class="S14"><input type="text" class="input size2 W90_" name="fsjtime" id="fsjtime" value="<?php echo YmdHis($sjtime); ?>" placeholder="请输入起始时间" /></td>
    </tr>
    <tr>
        <td width="90" align="right" class="S14">VIP结束时间 </td>
      <td align="left" class="S14"><input type="text" class="input size2 W90_" name="fsjtime2" id="fsjtime2" value="<?php echo YmdHis($sjtime2); ?>" placeholder="请输入结束时间" /></td>
    </tr>
    <tr>
        <td width="90" align="right" class="S14">用户等级</td>
        <td align="left">
        <?php
		sort($urole);
		foreach($urole as $RV){
			//if($RV['f']==0)continue;
			?>
            <div class="gradelist">
              <input type="radio" name="fgrade" id="fgrade<?php echo $RV['g'];?>" class="radioskin" value="<?php echo $RV['g'];?>"<?php echo ($RV['g'] == $grade)?' checked':'';?>>
              <label for="fgrade<?php echo $RV['g'];?>" class="radioskin-label"><i class="i1"></i><b class="W120 S14"><?php echo uicon_grade_all($RV['g'],1,$sex).$RV['t'];?></b></label>
            </div>
        <?php }?>
        </td>
    </tr>
</table>
<input type="hidden" name="submitok" value="mod" /><input type="hidden" name="uid" value="<?php echo $uid;?>" />
<br><br><div class="savebtnbox"><button type="submit" id="submit_add" class="btn size3">确定并保存</button></div>
</form>
<script>
zeai.listEach(zeai.tag(autodate,'a'),function(obj){
	obj.onclick=function(){
		o(fsjtime).value=obj.getAttribute("d1");
		o(fsjtime2).value=obj.getAttribute("d2");
	}
});
</script>
<?php require_once 'bottomadm.php';?>